package vip.ola.pay.channel.jdpay;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.entity.PayOrder;
import vip.ola.core.entity.PayPassageAccount;
import vip.ola.pay.channel.abc.AbcPaymentService;
import vip.ola.pay.channel.dszfb.DszfbPaymentService;
import vip.ola.pay.ctrl.common.BaseController;
import vip.ola.pay.mq.BaseNotify4MchPay;
import vip.ola.pay.service.RpcCommonService;

import java.math.BigDecimal;

import static vip.ola.core.common.constant.PayConstant.PAY_STATUS_COMPLETE;
import static vip.ola.core.common.constant.PayConstant.PAY_STATUS_SUCCESS;

@RequestMapping("/api/jdwx")
@Controller
public class JdwxPayController extends BaseController {
    private static final MyLog _log = MyLog.getLog(JdwxPayController.class);

    @Autowired
    private RpcCommonService rpcCommonService;
    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    @RequestMapping("/notify")
    @ResponseBody
    public String notify(@RequestBody JSONObject jsonObject){
        _log.info("接收jdwx回调：" + jsonObject.toString());
        String mchOrderNo = jsonObject.getString("out_trade_no");
        PayOrder payOrder = rpcCommonService.rpcPayOrderService.findByPayOrderId(mchOrderNo);
        if(payOrder == null){
            return "order not exist!";
        }
        Long orderMoney = new BigDecimal(jsonObject.getString("money")).longValue();
        if (orderMoney.longValue() != payOrder.getAmount()/100){
            return "money fail";
        }
        if (payOrder.getStatus() == PAY_STATUS_SUCCESS || payOrder.getStatus() == PAY_STATUS_COMPLETE) {
            return "SUCCESS";
        }
        PayPassageAccount payPassageAccount = rpcCommonService.rpcPayPassageAccountService.findById(payOrder.getPassageAccountId());
        if(payPassageAccount == null || StringUtils.isBlank(payPassageAccount.getParam())){
            return "fail";
        }
        if ("2".equals(jsonObject.getString("status"))) {
            String retSign = jsonObject.getString("sign");
            jsonObject.remove("sign");
            String sign = JdwxPaymentService.getSign(jsonObject, payPassageAccount.getPrivateKey()).toUpperCase();
            _log.info("加签信息：map-{},retSign->{},sign->{}", jsonObject,retSign, sign );
            if (retSign.equals(sign)) {
                int result = rpcCommonService.rpcPayOrderService.updateStatus4Success(payOrder.getPayOrderId());
                _log.info("更新订单状态为支付成功: result={}", result);
                if(result == 1){
                    // 通知业务系统
                    baseNotify4MchPay.doNotify(payOrder, true);
                    return "SUCCESS";
                }
            }
        }
        return "fail";
    }
}

